%***********************************************************
% CS 229 Machine Learning
% Project, Ground truth data generation
%-----------------------------------------------------------
% Date : November 13, 2010
%***********************************************************

clear all;%test
addpath(genpath('./KalmanAll/'))

%**************************************************************************
%Input parameters
outputwidth = 320;
outputheight = 180;
Wmax = 640;
Hmax = 360;
scale_conversion_factor_tracking = 1.5;

lecturer_tracking_scale_factor = 640/2560;
saliency_tracking_scale_factor = 640/1920;

load groundTruth.dat;
load lecturer.dat;
load saliency.dat;
load boards.dat;
%**************************************************************************

% saliency = saliency22;
% boards = boards22;
% lecturer = lecturer22;

dataA = groundTruth;

% The required frame rate is 33 ms per frame. Our data contains observations 
% that are taken at time intervals that are multiples of 100 ms. Thus, we 
% need to interpolate these observations. 

size(dataA)

%--------------------------------------------------------------------------
% Interpolate data
interpolatedTimeStamps = (dataA(1,1):100/3:dataA(size(dataA,1),1));
newDataA = zeros(size(interpolatedTimeStamps',1),3);
newDataA(:,1) = interpolatedTimeStamps';
newDataA(:,2) = (interp1(dataA(:,1)', dataA(:,2)', interpolatedTimeStamps))'./scale_conversion_factor_tracking;
newDataA(:,3) = (interp1(dataA(:,1)', dataA(:,3)', interpolatedTimeStamps))'./scale_conversion_factor_tracking;

%--------------------------------------------------------------------------
%Adjustment for camera border case
% newDataA(:,2) = min(max(floor(newDataA(:,2) - outputwidth/2),1),Wmax-outputwidth) + outputwidth/2;
% newDataA(:,3) = min(max(floor(newDataA(:,3) - outputheight/2),1),Hmax-outputheight) +outputheight/2 ;
%--------------------------------------------------------------------------

%==========================================================================================

% For now, train and test using the groundth truth co-ordinates as training values instead of u_c
y = newDataA(:,2);
y = smooth(y,150);

% Prepare features first
size(lecturer)
size(saliency)
size(y)

% Scale vectors
%lecuturer feature adjustment
lecturer = lecturer * lecturer_tracking_scale_factor; 
% lecturer(:,2) = min(max(floor(lecturer(:,2) - outputwidth/2),1),Wmax-outputwidth) + outputwidth/2;
% lecturer(:,3) = min(max(floor(lecturer(:,3) - outputheight/2),1),Hmax-outputheight) +outputheight/2 ;

%saliency feature adjustment
saliency = saliency *  saliency_tracking_scale_factor;
%saliency(:,2) = saliency(:,2) + outputwidth/2;
%saliency(:,3) = saliency(:,3) + outputheight/2;

panning_indicator = zeros(size(y));
panning_indicator(y(2:end)~= y(1:end-1)) = 1;

%remove noisy panning 
panning_indicator= smooth(panning_indicator,400);
panning_indicator(panning_indicator>0.5)= 1;
panning_indicator(panning_indicator<=0.5)= -1;

figure(1)
t = 1:50000;
plot(t, 300*panning_indicator(t), t, y(t));

save('panning.mat','panning_indicator');





